﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
   public  class Process
    {
       MeKongDataDataContext meKongDataContext;

       public Process()
       {
           meKongDataContext = new MeKongDataDataContext();
       }

       public List<LoaiSP> GetAllLoaiSP()
       {
           var res = from loaiSP in meKongDataContext.LoaiSPs
                     select loaiSP;
           return res.ToList<LoaiSP>();
       }

       public List<SanPham> GetSanPhamByMLSP(int maLoaiSP)
       {
           var res = from sp in meKongDataContext.SanPhams
                     where sp.MaLoaiSP == maLoaiSP
                     select sp;
           return res.ToList<SanPham>();
       }

       public List<SanPham> GetSanPhamByNSX(int maNSX)
       {
           var res = from sp in meKongDataContext.SanPhams
                     where sp.MaNSX == maNSX
                     select sp;
           return res.ToList<SanPham>();
       }
       public List<SanPham> GetAllSP()
       {
           var res = from sp in meKongDataContext.SanPhams
                     select sp;
           return res.ToList<SanPham>();
       }

       public List<SanPham> GetSPByName(string tenSp)
       {
           var res = from sp in meKongDataContext.SanPhams
                     where sp.TenSP.Contains(tenSp)
                     select sp;
           return res.ToList<SanPham>();
       }
       public List<NhaSX> GetAllNSX()
       {
           var res = from sp in meKongDataContext.NhaSXes
                     select sp;
           return res.ToList<NhaSX>();
       }

       public KhachHang GetObjectByID(int maKH)
       {
           var res = meKongDataContext.KhachHangs.Single(r => r.MaKH == maKH);
           return res;
       }

       public KhachHang GetObjectByUserName(string userName)
       {
           var res = meKongDataContext.KhachHangs.Single(r=> r.UserName == userName);
           return res;
       }

       public KhachHang Login(string userName, string pass)
       {
           var res = from kh in meKongDataContext.KhachHangs
                     where  kh.UserName == userName &&  kh.PassWork == pass
                     select kh;
           return res.FirstOrDefault();
       }

       public KhachHang InserKhachHang(KhachHang objKhachHang)
       {
           meKongDataContext.KhachHangs.InsertOnSubmit(objKhachHang);
           meKongDataContext.SubmitChanges();
           return objKhachHang;
       }

       public KhachHang UpdataKhacHang(KhachHang objKhachHang) 
       {
           var res = meKongDataContext.KhachHangs.Single(r=>r.MaKH == objKhachHang.MaKH);
           res.MaKH = objKhachHang.MaKH;
           res.PassWork = objKhachHang.PassWork;
            meKongDataContext.SubmitChanges();
            return objKhachHang;
       }

       public SanPham InsertSanPham(SanPham objSanPham)
       {
           meKongDataContext.SanPhams.InsertOnSubmit(objSanPham);
           meKongDataContext.SubmitChanges();
           return objSanPham;
       }

       public SanPham UpdateSanPham(SanPham objSanPham)
       {
           var res = meKongDataContext.SanPhams.Single(r => r.MaSP == objSanPham.MaSP);
           res.MaSP = objSanPham.MaSP;
           res.TenSP = objSanPham.TenSP;
           res.ThongSo = objSanPham.ThongSo;
           meKongDataContext.SubmitChanges();
           return objSanPham;
       }

       public Banking UpdateBanking(Banking objBanking)
       {
           var res = meKongDataContext.Bankings.Single(r => r.CustomerID == objBanking.CustomerID);
           res.CustomerID = objBanking.CustomerID;
           res.Baland = objBanking.Baland;
           meKongDataContext.SubmitChanges();
           return objBanking;
       }

       public GioHang TaoMoiGioHang(GioHang objGioHang)
       {
           GioHang gh = new GioHang();
           meKongDataContext.GioHangs.InsertOnSubmit(objGioHang);
           meKongDataContext.SubmitChanges();
           return objGioHang;
       }

       public ChiTietGioHang TaoMoiChiTietGioHang(ChiTietGioHang objChiTietGioHang)
       {
           ChiTietGioHang ctGioHang = new ChiTietGioHang();
           meKongDataContext.ChiTietGioHangs.InsertOnSubmit(objChiTietGioHang);
           meKongDataContext.SubmitChanges();
           return objChiTietGioHang;
       }

       public Banking GetBanking(string userName, string pass)
       {
           var res = from kh in meKongDataContext.Bankings
                     where kh.UserNameBank == userName && kh.PassworkBank == pass
                     select kh;
           return res.FirstOrDefault();
       }
    }
}
